/*  File Name	: Timer.cpp
 *  Author		: Pang Jiacai
 *	 E-mail		: 823960168@qq.com
 *  Created on	: 2012-6-4
 *  Description:
 */

#include "Timer.h"
#include "Reactor.h"

Timer::Timer(Reactor *reactor,std::string name)
{
	this->timer_name_ = name;
	this->timer_id_=-1;
	this->reactor(reactor);
}

Timer::~Timer(void)
{

}

long Timer::timer_id(void) const
{
	return this->timer_id_;
}

std::string Timer::name(void)
{
	return this->timer_name_;
}

int Timer::handle_timeout(void)
{
	return 0;
}

int Timer::schedule_timer(TimeValue delay,TimeValue interval)
{
	this->timer_id_ = this->reactor()->schudle_timer(this,delay,interval);
	return this->timer_id_;
}

int Timer::cancel_timer(void)
{
	if(this->reactor()->cancel_timer(this->timer_id_)==0)
	{
		this->timer_id_ =-1;
		return 0;
	}
	return -1;
}

int Timer::refresh_timer(TimeValue time)
{
	if(this->timer_id_==-1 || (time.sec()==0 && time.msec()==0))
	{
		return -1;
	}
	return this->reactor()->refresh_timer(this->timer_id_,time);
}
